
var timeConstraints = {
    timePattern: 'HH:mm:ss',
    clickableIncrement: 'T00:15:00',
    visibleIncrement: 'T00:15:00',
    visibleRange: 'T01:00:00'
};

var dateConstraints = {
    datePattern: "yyyy-MM-dd"
};

var pathStore;

dojo.addOnLoad(function() {
    var tz = getTimezoneName();
    dijit.byId('timezone').attr('content', '<p style="font-size: 80%; text-align:right"> Times are in your timezone ('+tz+')</p>');
    var now = new Date();
    var fivemin = 5*60*1000;
    var upTime = now.getTime() - (now.getTime() % fivemin) + fivemin;
    var dnTime = upTime + fivemin;
    
    var upDt = new Date(upTime)
    var dnDt = new Date(dnTime)
    
    new dijit.form.TimeTextBox({
        name: "upTime",
        value: upDt,
        constraints: timeConstraints,
        style: {
            width: "80px",
        },
    },
    "upTime");
    new dijit.form.TimeTextBox({
        name: "dnTime",
        value: dnDt,
        constraints: timeConstraints,
        style: {
            width: "80px",
        },
    },
    "dnTime");
    new dijit.form.DateTextBox({
        id: "upDate",
        name: "upDate",
        value: new Date(),
        constraints: dateConstraints,
        style: {
            width: "80px",
        },
    },
    "upDate");
    new dijit.form.DateTextBox({
        id: "dnDate",
        name: "dnDate",
        value: new Date(),
        constraints: dateConstraints,
        style: {
            width: "80px",
        },
    },
    "dnDate");
    var props = {
        name: "vlan",
        constraints: {
            min: 2,
            max: 4095,
            pattern: "####"
        },
        value: 100
    };
    new dijit.form.NumberTextBox(props, "srcVlan");
    new dijit.form.NumberTextBox(props, "dstVlan");
    var srcSelectDiv = dijit.byId("srcSelectDiv");
    var dstSelectDiv = dijit.byId("dstSelectDiv");

    var slider = new dijit.form.HorizontalSlider({
        name: "slider",
        value: 1000,
        minimum: 100,
        maximum: 10000,
        intermediateChanges: true,
        discreteValues: 100,
        style: "width:300px;",
        onChange: function(value) {
            dojo.byId("sliderValue").value = value;
        }
    },
    "slider");
    
    dijit.byId('srcGrpSelect').onChange = function(group) {
        dijit.byId('srcSelect').query.group = group || "*";
        dijit.byId('srcSelect').textbox.value = "";
        dijit.byId('srcSelect').valueNode.value = "";
        dijit.byId('srcSelect').value = "";
    }
    dijit.byId('dstGrpSelect').onChange = function(group) {
        dijit.byId('dstSelect').query.group = group || "*";
        dijit.byId('dstSelect').textbox.value = "";
        dijit.byId('dstSelect').valueNode.value = "";
        dijit.byId('dstSelect').value = "";
    }
    dijit.byId('srcNetSelect').onChange = function(network) {
        dijit.byId('srcSelect').query.network = network || "*";
        dijit.byId('srcSelect').textbox.value = "";
        dijit.byId('srcSelect').valueNode.value = "";
        dijit.byId('srcSelect').value = "";
        dijit.byId('srcGrpSelect').query.network = network || "*";
        dijit.byId('srcGrpSelect').value = "";
        dijit.byId('srcGrpSelect').valueNode.value = "";
        dijit.byId('srcGrpSelect').textbox.value = "";
    };
    dijit.byId('dstNetSelect').onChange = function(network) {
        dijit.byId('dstSelect').query.network = network || "*";
        dijit.byId('dstSelect').textbox.value = "";
        dijit.byId('dstSelect').valueNode.value = "";
        dijit.byId('dstSelect').value = "";
        dijit.byId('dstGrpSelect').query.network = network || "*";
        dijit.byId('dstGrpSelect').value = "";
        dijit.byId('dstGrpSelect').valueNode.value = "";
        dijit.byId('dstGrpSelect').textbox.value = "";
    };
    
    
    pathStore = new dojo.data.ItemFileReadStore({data: {items:[]}});

    var grid = dijit.byId("pathGrid");
    grid.setStore(pathStore, null, null);
    grid.update();

    var jsonGraph = dojo.xhrGet({
        url: "graph", 
        handleAs: "json", 
        load: function(response, ioArgs) {
            graphCallback(response);
        }
    });
});

function graphCallback(jsonGraph) {
    drawGraph(jsonGraph);
}

function populatePath() {
    var pathQuery;
    
    var src = dijit.byId('srcSelect').attr('value');
    var dst = dijit.byId('dstSelect').attr('value');
    

    pathStore = new dojo.data.ItemFileReadStore({url: '/path?src='+src+'&dst='+dst});
    var grid = dijit.byId("pathGrid");
    grid.setStore(pathStore, null, null);

}

function submitReserve() {
    tmWinter = new Date(Date.UTC(2010, 12, 30, 0, 0, 0, 0));
    gmtOffset = -60000 * tmWinter.getTimezoneOffset();
    
    
    var tabs = dijit.byId("tabs");
    var queryTab = dijit.byId("queryTab");
    var upDate =  dijit.byId('upDate').attr('value').getTime();
    var upTime =  dijit.byId('upTime').attr('value').getTime();
    var dnDate =  dijit.byId('dnDate').attr('value').getTime();
    var dnTime =  dijit.byId('dnTime').attr('value').getTime();
    var upMillis = upDate+upTime + gmtOffset;
    var dnMillis = dnDate+dnTime + gmtOffset;

    var submitContent = {
        src: dijit.byId('srcSelect').attr('value'), 
        dst: dijit.byId('dstSelect').attr('value'), 
        
        srcNet: dijit.byId('srcNetSelect').attr('value'), 
        dstNet: dijit.byId('dstNetSelect').attr('value'), 
        
        
        srcVlan: dijit.byId('srcVlan').attr('value'),

        /* ATTENTION - no VLAN translation so we just reuse the source VLAN*/
        dstVlan: dijit.byId('srcVlan').attr('value'),
        bandwidth: dijit.byId('sliderValue').attr('value'),
        upMillis: upMillis,
        dnMillis: dnMillis,
    }
    tabs.selectChild(queryTab);
    
    dojo.query("#statusResult p").forEach(loadFromQuery("/create", submitContent));
}

/* utility stuff */
function loadIntoNode(data, xhr){
    if(xhr.args.node){
      xhr.args.node.innerHTML = data;
    }
}

function intoNode(node, url){
    return {
      url: url,
      node: dojo.byId(node),
      load: loadIntoNode
    };
}

function loadFromQuery(url, content){
    var get = dojo.xhrPost({ url: url, content: content });
    return function(node){
      get.addCallback(function(data){
        node.innerHTML = data;
        return data;
      });
    }
}

function getTimezoneName() {
    tmSummer = new Date(Date.UTC(2010, 6, 30, 0, 0, 0, 0));
    so = -1 * tmSummer.getTimezoneOffset();
    tmWinter = new Date(Date.UTC(2010, 12, 30, 0, 0, 0, 0));
    wo = -1 * tmWinter.getTimezoneOffset();

    if (-660 == so && -660 == wo) return 'Pacific/Midway';
    if (-600 == so && -600 == wo) return 'Pacific/Tahiti';
    if (-570 == so && -570 == wo) return 'Pacific/Marquesas';
    if (-540 == so && -600 == wo) return 'America/Adak';
    if (-540 == so && -540 == wo) return 'Pacific/Gambier';
    if (-480 == so && -540 == wo) return 'US/Alaska';
    if (-480 == so && -480 == wo) return 'Pacific/Pitcairn';
    if (-420 == so && -480 == wo) return 'US/Pacific';
    if (-420 == so && -420 == wo) return 'US/Arizona';
    if (-360 == so && -420 == wo) return 'US/Mountain';
    if (-360 == so && -360 == wo) return 'America/Guatemala';
    if (-360 == so && -300 == wo) return 'Pacific/Easter';
    if (-300 == so && -360 == wo) return 'US/Central';
    if (-300 == so && -300 == wo) return 'America/Bogota';
    if (-240 == so && -300 == wo) return 'US/Eastern';
    if (-240 == so && -240 == wo) return 'America/Caracas';
    if (-240 == so && -180 == wo) return 'America/Santiago';
    if (-180 == so && -240 == wo) return 'Canada/Atlantic';
    if (-180 == so && -180 == wo) return 'America/Montevideo';
    if (-180 == so && -120 == wo) return 'America/Sao_Paulo';
    if (-150 == so && -210 == wo) return 'America/St_Johns';
    if (-120 == so && -180 == wo) return 'America/Godthab';
    if (-120 == so && -120 == wo) return 'America/Noronha';
    if (-60 == so && -60 == wo) return 'Atlantic/Cape_Verde';
    if (0 == so && -60 == wo) return 'Atlantic/Azores';
    if (0 == so && 0 == wo) return 'Africa/Casablanca';
    if (60 == so && 0 == wo) return 'Europe/London';
    if (60 == so && 60 == wo) return 'Africa/Algiers';
    if (60 == so && 120 == wo) return 'Africa/Windhoek';
    if (120 == so && 60 == wo) return 'Europe/Amsterdam';
    if (120 == so && 120 == wo) return 'Africa/Harare';
    if (180 == so && 120 == wo) return 'Europe/Athens';
    if (180 == so && 180 == wo) return 'Africa/Nairobi';
    if (240 == so && 180 == wo) return 'Europe/Moscow';
    if (240 == so && 240 == wo) return 'Asia/Dubai';
    if (270 == so && 210 == wo) return 'Asia/Tehran';
    if (270 == so && 270 == wo) return 'Asia/Kabul';
    if (300 == so && 240 == wo) return 'Asia/Baku';
    if (300 == so && 300 == wo) return 'Asia/Karachi';
    if (330 == so && 330 == wo) return 'Asia/Calcutta';
    if (345 == so && 345 == wo) return 'Asia/Katmandu';
    if (360 == so && 300 == wo) return 'Asia/Yekaterinburg';
    if (360 == so && 360 == wo) return 'Asia/Colombo';
    if (390 == so && 390 == wo) return 'Asia/Rangoon';
    if (420 == so && 360 == wo) return 'Asia/Almaty';
    if (420 == so && 420 == wo) return 'Asia/Bangkok';
    if (480 == so && 420 == wo) return 'Asia/Krasnoyarsk';
    if (480 == so && 480 == wo) return 'Australia/Perth';
    if (540 == so && 480 == wo) return 'Asia/Irkutsk';
    if (540 == so && 540 == wo) return 'Asia/Tokyo';
    if (570 == so && 570 == wo) return 'Australia/Darwin';
    if (570 == so && 630 == wo) return 'Australia/Adelaide';
    if (600 == so && 540 == wo) return 'Asia/Yakutsk';
    if (600 == so && 600 == wo) return 'Australia/Brisbane';
    if (600 == so && 660 == wo) return 'Australia/Sydney';
    if (630 == so && 660 == wo) return 'Australia/Lord_Howe';
    if (660 == so && 600 == wo) return 'Asia/Vladivostok';
    if (660 == so && 660 == wo) return 'Pacific/Guadalcanal';
    if (690 == so && 690 == wo) return 'Pacific/Norfolk';
    if (720 == so && 660 == wo) return 'Asia/Magadan';
    if (720 == so && 720 == wo) return 'Pacific/Fiji';
    if (720 == so && 780 == wo) return 'Pacific/Auckland';
    if (765 == so && 825 == wo) return 'Pacific/Chatham';
    if (780 == so && 780 == wo) return 'Pacific/Enderbury'
    if (840 == so && 840 == wo) return 'Pacific/Kiritimati';
    return 'US/Pacific';
}

    